package com.dream.classcontrol;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.Window;
import android.widget.EditText;
import android.widget.Toast;

import com.dream.sdkmanager.SDKManager;

/**
 * Created by Administrator on 2017/8/16.
 */

public class SettingActivity extends Activity implements View.OnClickListener, SDKManager.onGetData {

    private EditText editId;
    private EditText editClassname;
    private EditText weizonIp;
    private SharedPreferences sharedPreferences;
    private SharedPreferences.Editor editor;
    private EditText editSdkReConn;
    private SDKManager sdkManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_setting);
        initView();
    }

    public void initView(){
        sdkManager=new SDKManager(this);
        sdkManager.setSdkDataListener(this);
        sharedPreferences=getSharedPreferences("appinfo",MODE_PRIVATE);
        editor=sharedPreferences.edit();
        editId = (EditText) findViewById(R.id.edit_id);
        editClassname = (EditText) findViewById(R.id.edit_classname);
        weizonIp = (EditText) findViewById(R.id.weizon_ip);
        editSdkReConn = (EditText) findViewById(R.id.edit_sdkReConn);
        editId.setText(sharedPreferences.getString("id",""));
        editClassname.setText(sharedPreferences.getString("className",""));
        weizonIp.setText(sdkManager.getWeizonIp());
        editSdkReConn.setText(sdkManager.getSdkReConn());

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.search:
                sdkManager.initSearch();
                break;
            case R.id.save:
                editor.putString("id",editId.getText().toString().trim());
                editor.putString("className",editClassname.getText().toString().trim());
                if (!editSdkReConn.getText().toString().trim().equals("")){
                    sdkManager.setSdkReConn(Integer.parseInt(editSdkReConn.getText().toString().trim()));
                }
                editor.commit();
                Toast.makeText(getApplication(),"保存成功",Toast.LENGTH_SHORT).show();
                startActivity(new Intent(SettingActivity.this,MainActivity.class));
                finish();
                break;
            case R.id.cancle:
                startActivity(new Intent(SettingActivity.this,MainActivity.class));
                finish();
                break;
        }
    }

    @Override
    public void onSearchFinish(String weiZoneIp) {
        weizonIp.setText(weiZoneIp);
    }

    @Override
    public void getLandState(boolean isLand,String hostId) {

    }

    @Override
    public void getDevData(String UIID, String VALUE, String RFID, String INDEX, String DEVID, String HOST) {

    }

    @Override
    public void getSetVarState(String s, String s1, String s2) {

    }

    @Override
    public void getSetKeyState(String s, String s1, String s2) {

    }

}
